package com.xmut.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.xmut.entity.Comment;
import com.xmut.filter.CommentFilter;
import com.xmut.service.CommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/modules/xmut/comment")
public class CommentController extends JsonBaseController {

    @Autowired
    private CommentService commentService;

    @Override
    protected String getView(String view) {
        return String.format("/modules/xmut/comment/%s",view);
    }

    /**
     * 初始化界面
     *
     * @return url
     */
    @RequiresPermissions("modules:xmut:comment:query")
    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    /**
     * 输出列表
     *
     * @param filter
     * @return json
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:comment:query")
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute CommentFilter filter) {
        List<Comment> comments = commentService.query(filter);
        return coverJqGrid(filter, comments);
    }

    /**
     * 跳转至filter界面
     * @param filter
     * @return
     */
    @RequestMapping("/filter")
    @RequiresPermissions("modules:xmut:comment:query")
    public String filter(@ModelAttribute CommentFilter filter) {
        return getView("filter");
    }

    /**
     * 执行创建
     * @param comment
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:comment:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute Comment comment) {
        commentService.save(comment);
        return coverMessage("200");
    }

    /**
     * 初始化创建界面
     * @param model
     * @return url
     */
    @RequiresPermissions("modules:xmut:comment:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute  Comment comment, Model model) {
        return getView("form");
    }

    /**
     * 初始化更新界面
     *
     * @param model
     * @param id
     * @return url
     */
    @RequiresPermissions("modules:xmut:comment:edit")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable Long id) {
        Comment comment = commentService.getById(id);
        model.addAttribute("comment", comment);
        return getView("form");
    }

    /**
     * 更新操作
     * @param comment
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:comment:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute Comment comment) {
        commentService.update(comment);
        return coverMessage("200");
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:comment:delete")
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<Long> id) {
        commentService.batchDelete(id);
        return coverMessage("200");
    }
}
